<?php
/**
 * 公文管理模型
 *
 */
class Model_Document extends Model_Base{
	
	private $userTable = '';
	private $departmentTable = '';
	private $orderCarTable = '';
	private $documentTable = '';
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->userTable = tabname('user');
		$this->departmentTable = tabname('department');
		$this->orderCarTable = tabname('order_car');
		$this->documentTable = tabname('document');
	}
	
	public function countDocument(){
		return $this->db->count($this->documentTable,array('is_deleted'=>0));
	}
	
	public function listDocument($start=null,$length=null){
		$sql = "SELECT * FROM ".$this->documentTable." WHERE is_deleted = 0 ORDER BY id DESC";
		if($length > 0){
			$sql .= " LIMIT $start,$length";
		}
		$lists = $this->db->fetchAll($sql);
		foreach ($lists as $key=>$row){
			$lists[$key]['num'] = "DOC".str_pad(intval($row['id']), 10, "0", STR_PAD_LEFT);
		}
		return $lists;
	}
	
	public function addDocument($sets){
	$this->db->insert($this->documentTable,$sets);
		$document_id = $this->db->lastInsertId();
		if($document_id>0){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function updateDocument($id,$sets){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->documentTable,$sets,array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
	public function delDocument($id){
		if(empty($id)){
			return false;
		}else{
			$ret = $this->db->update($this->documentTable,array('is_deleted'=>1),array('id'=>$id));
			if($ret){
				return true;
			}
			else{
				return false;
			}
		}
	}
}
